package com.wdb.modules.mnt.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wdb.modules.mnt.domain.Database;
import com.wdb.modules.mnt.domain.vo.DatabaseQueryCriteria;
import com.wdb.modules.mnt.mapper.DatabaseMapper;
import com.wdb.modules.mnt.service.DatabaseService;
import com.wdb.modules.mnt.util.SqlUtils;
import com.wdb.utils.PageResult;
import com.wdb.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author: wendaobai@qq.com
 * @date: 2024/9/4 星期三 10:27
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DatabaseServiceImpl extends ServiceImpl<DatabaseMapper, Database>
    implements DatabaseService {
  private  final  DatabaseMapper databaseMapper;
  @Override
  public boolean testConnect(Database resources) {
    try {
      return SqlUtils.testConnection(
          resources.getJdbcUrl(), resources.getUserName(), resources.getPwd());
    } catch (Exception e) {
      log.error(e.getMessage());
      return false;
    }
  }

  @Override
  public void create(Database resources) {
    resources.setId(IdUtil.simpleUUID());
    save(resources);
  }

  @Override
  public PageResult<Database> queryAll(DatabaseQueryCriteria criteria, Page page) {

    return PageUtil.toPage(databaseMapper.findAll(criteria,page));
  }

  @Override
  public Database findById(String id) {
    return databaseMapper.selectById(id);
  }
}
